package edoc.dao.impl;

import java.util.List;

import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import edoc.dao.EntryDao;
import edoc.entity.EdocCategory;
import edoc.entity.EdocEntry;

public class EntryDaoImpl extends HibernateDaoSupport implements EntryDao {
	/**
	 * 查询全部电子文档信息
	 * @return 文档列表
	 */
	@Override
	public List<EdocEntry> findAll(Long id) {
		List<EdocEntry> entryList=null;
		
		try {
			StringBuffer sb=new StringBuffer();
			sb.append("from EdocEntry  where  1=1  ");
			if(id!=null&&id!=-1){
				sb.append("  and edocCategory.id="+id);
			}
			sb.append("  order by id desc");
			entryList=this.getHibernateTemplate().find(sb.toString());
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entryList;
	}

	@Override
	/***
	 * 添加电子文档信息
	 */
	public boolean addEntry(EdocEntry entry) {
		try {
			this.getHibernateTemplate().save(entry);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	/***
	 * 删除电子文档信息根据id
	 */
	public boolean del(Long id) {
		try {
			//通过id先查询出一条记录
			EdocEntry entry=this.getHibernateTemplate().get(EdocEntry.class, id);
			//调用delete方法进行删除
			this.getHibernateTemplate().delete(entry);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	/**
	 * 修改电子文档
	 * @param entry
	 * @return
	 */
	@Override
	public boolean update(EdocEntry entry) {
		try {
			this.getHibernateTemplate().update(entry);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	
	/**
	 * 根据id查询电子文档
	 * @param id
	 * @return 实体对象edocEntry
	 */
	@Override
	public EdocEntry findById(Long id) {
		EdocEntry entry=null;
		try {
			entry=this.getHibernateTemplate().get(EdocEntry.class, id);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entry;
	}

	@Override
	public List<EdocCategory> findCategory() {
		List<EdocCategory> categoryList=null;
		try {
			categoryList=this.getHibernateTemplate().find("from EdocCategory");
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		return categoryList;
	}

}
